-
Notifications
You must be signed in to change notification settings - Fork 754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Re-initialize the cache if the directory was deleted. #297
Conversation
If the user clears the cache from the Android Settings page, the DiskBasedCache's root directory is deleted. This means that the app will be running without a cache until it is restarted and the cache initialized. This fix initializes the cache when it finds that the root directory no longer exists. Note that the first entry after deletion that is put into the cache is still lost, the cache is only re-initialized when putting that entry fails. Adding retries would be more complicated, since we would have to avoid getting into a loop if creating the root directory fails.
@Test | ||
public void initializeIfRootDirectoryDeleted() { | ||
temporaryFolder.delete(); | ||
DiskBasedCache uninitializedCache = new DiskBasedCache(temporaryFolder.getRoot(), MAX_SIZE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit - in the spirit of testing more representative behavior, should this unit test perhaps create the cache, call initialize(), and only then delete the temporary folder?
The behavior tested here isn't a supported sequence in general - we don't expect the cache to work if you don't initialize it. It happens to hit the code path you want, but that's not necessarily reliable. On the other hand, we know user devices hit the scenario that an already-initialized cache has the directory deleted from underneath it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Rather than testing with an uninitialized test, the test should use an initialized cache, delete the root directory, and then confirm that the cache is re-initialized.
@@ -258,10 +258,11 @@ public synchronized void put(String key, Entry entry) { | |||
pruneIfNeeded(); | |||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return; |
If move logic inside the catch block, then remove the return that is no longer needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
After the other code was moved into the catch statement, this return is no longer needed.
Thanks for the change and the review! |
Re-initialize the cache if the directory was deleted. (google#297)
If the user clears the cache from the Android Settings page, the
DiskBasedCache's root directory is deleted. This means that the app will
be running without a cache until it is restarted and the cache
initialized.
This fix initializes the cache when it finds that the root directory no
longer exists.
Note that the first entry after deletion that is put into the cache is
still lost, the cache is only re-initialized when putting that entry
fails. Adding retries would be more complicated, since we would have to
avoid getting into a loop if creating the root directory fails.